import Vue from 'vue'
import VueRouter from 'vue-router'
import request from '../axios/index'
import store from '@/store'
import { faClose } from '@fortawesome/free-solid-svg-icons'
Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    meta:{
      title:'主页',
      isAuth:false,
      keepAlive:true,
      scrollTop:0,
      naviState:false,
      naviBackColorState:false,
    },
    component: () => import('@/view/Home/Home.vue')
  },
  {
    path: '/message',
    name: 'message',
    meta:{
      title:'消息',
      isAuth:true,
      naviState:true,
      naviBackColorState:true,
    },
    component: () => import('@/view/Message/Message.vue')
  },
    {
      path: '/square',
      name: 'square',
      meta:{
        title:'广场',
        isAuth:false,
        naviState:true,
        keepAlive:true,
         naviBackColorState:false,
      },
      component: () => import('@/view/Square/Square.vue')
    },
    {
      path: '/personal',
      name: 'personal',
      meta:{
        title:'个人中心',
        isAuth:false,
        naviState:true,
        naviBackColorState:true,
      },
      component: () => import('@/view/Personal/Personal.vue')
    },
    {
      path: '/articleDetail',
      name: 'articleDetail',
      meta:{
        title:'文章详情',
        naviState:false,
        naviBackColorState:false,
      },
      component: () => import('@/view/ArticleDetail/ArticleDetail.vue')
    },
    {
      path: '/search',
      name: 'search',
      meta:{
        title:'搜索',
        naviState:true,
        naviBackColorState:false,

      },
      component: () => import('@/view/Search/Search.vue')
    },
    {
      path: '/error',
      name: 'error',
      meta:{
        title:'错误',
        naviState:false,
        naviBackColorState:false,
        
      },
      component: () => import('@/view/Error/Error.vue')
    },
 
  {
    path: '*',
    name: 'home',
    meta:{
      title:'主页',
      isAuth:false,
      naviState:false,
      naviBackColorState:false,
    },
    component: () => import('@/view/Home/Home.vue')
  }
]

const router = new VueRouter({
      routes
})

// 判断主页权限以及是否合法
async function judgePersonal(to,from,next){
    if(to.query.uid==null  || typeof to.query.uid !='string')
    return store.commit('Tool/NOTICEON',{message:'不要随意修改路径',type:'warn',title:'警告'})

    let User=JSON.parse(localStorage.getItem('User'))
    let uid= User ?  User.uid : 0
    let homepageuid = to.query ? to.query.uid : 0
    let res= await store.dispatch('Personal/getPersonalData',{ homepageuid, uid  })
    if(res.code!=200) {
      store.commit('Error/SETERROR',res)
      return next({name:'error', replace: true})
    } 
    else to.meta.title = res.message + '的主页'
    return next()
} 
// token 判断
async function checkToken(to,from,next){
  let token=localStorage.getItem('token')
  if( token==null) return store.commit('Tool/NOTICEON',{message:'Token失效,请重新登录',type:'error',title:'错误'})
  let res = await   request.get("/api/check/token")
  if(res.data.code==-1) return store.commit('Tool/NOTICEON',{message:'Token失效,请重新登录',type:'error',title:'错误'})
  return next()
}
//文章详情判断
async function judgeArticleDetail(to,from,next){
  if(to.query.id==null  || typeof to.query.id !='string')
  return store.commit('Tool/NOTICEON',{message:'不要随意修改路径',type:'warn',title:'警告'})
  let User=JSON.parse(localStorage.getItem('User'))
  let createdUid= User?.uid || 0
  let id = to?.query.id || 0
  let res= await store.dispatch('ArticleDetail/GetArticleDetail',{ id, createdUid  })
  if(res.code!=200) {
    store.commit('Error/SETERROR',res)
    return router.push('/error')  
  } else to.meta.title = res.message
  return next()
} 

router.beforeEach( async (to,from,next)=>{

    store.commit('Tool/SETNAVIGATION',[to.meta.naviState,to.meta.naviBackColorState])
    //判断token
     if(to.meta.isAuth) return await checkToken(to,from,next)
    // 个人主页
    if(to.path=='/personal')  return await judgePersonal(to,from,next)    
    // 文章详情
    if(to.path=='/articleDetail')    return await judgeArticleDetail(to,from,next)


    return next()
})
router.afterEach((to,from)=>{
  document.title=to.meta.title
})
export default router
